<template>
  <div class="app-container">
    <el-card class="table_style">
      <el-form ref="ruleForm" :model="user" :rules="rules" label-width="100px">
        <el-form-item label="账号" prop="username">
          <el-input v-model="user.username" />
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input v-model="user.password" />
        </el-form-item>

        <el-form-item v-show="user.roleId !== null" label="用户组" prop="roleId">
          <el-select v-model="user.roleId" filterable placeholder="请选择用户组" style="width: 300px" :multiple-limit="2">
            <el-option
              v-for="role in roles"
              :key="role.id"
              :label="role.name"
              :value="role.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item v-show="user.roleId !== null" label="用户组" prop="roleId">
          <el-radio v-for="role in roles" :key="role.id" v-model="user.roleId" :label="role.id">
            {{ role.name }}
          </el-radio>
          <span style="color: red;margin-left: 100px;">* 两种写法随意，请勿同时使用！！！</span>
        </el-form-item>

        <el-form-item label="头像" prop="avatar">
          <el-upload
            class="avatar-uploader"
            :data="qiniuForm"
            :before-upload="beforeUpload"
            :on-success="uploadSuccess"
            action="https://upload.qiniup.com"
          >
            <img v-if="user.avatar" :src="user.avatar" class="avatar" alt="">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>

        <el-form-item v-if="user.roleId !== null" label="是否是管理员">
          <el-radio-group v-model="user.admin" size="medium">
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button @click="resetForm('ruleForm')">重置表单</el-button>
          <el-button type="primary" @click="submitForm('ruleForm')">{{ isEdit ? "立即更新" : "立即创建" }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { createUser, fetchUser, updateUser } from '@/api/user'
import user from '@/mixins/user'
import { fetchRoleList } from '@/api/role'

export default {
  name: 'UserForm',
  mixins: [user],
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      roles: []
    }
  },
  created() {
    this.init()
    // 如果是编辑
    if (this.isEdit) {
      this.fetchData()
    }
  },
  methods: {
    async init() {
      const res = await fetchRoleList()
      this.roles = res.data.roles
    },
    async fetchData() {
      const id = this.$route.params.id
      const res = await fetchUser(id)
      this.user = res.data.user
    },
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (!valid) return
        let res
        if (this.isEdit) {
          const id = this.$route.params.id
          res = await updateUser(id, this.user)
        } else {
          res = await createUser(this.user)
        }
        if (res.code === 20000) {
          this.$message({
            message: res.message,
            type: 'success'
          })
          this.$router.push({ name: 'User' })
        } else {
          this.$message({
            message: res.message,
            type: 'error'
          })
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
    },
    handleChange(value) {
    }
  }
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
  border: 1px dashed #d9d9d9;
}
.upload-demo {
  display: none;
}
</style>
